var db=null;
const version = '1.0';
const dbSize = 10 * 1024 * 1024;
function openComDB(name, path, callback) {
	uni.openDatabase({
		name: name,
		version: version,
		desc: 'my database',
		size: dbSize,
		 success: function (mydb) {
		        console.log('数据库创建或打开成功');
				db=mydb;
				/*
		        // 执行一个简单的查询
		        db.transaction(function (tx) {
		            tx.executeSql('CREATE TABLE IF NOT EXISTS testTable (id unique, name)', [], function (tx, result) {
		                console.log('表创建成功');
		            }, function (tx, error) {
		                console.log('创建表失败: ' + error.message);
		            });
		        });
				*/
		    },
		    fail: function (error) {
		        console.error('数据库创建或打开失败: ' + error.message);
		    }
	});
	//db = openDatabase(name, '1.0', '盘点数据库', 1024 * 1024*5,callback);
    /*
	plus.sqlite.openDatabase({  
        name: name,  
        path: path,  
        success: function(e) {  
            // plus.nativeUI.alert('打开数据库成功');  
            callback(e)  
        },  
        fail: function(e) {  
            // plus.nativeUI.alert("打开数据库失败");  
            callback(e);  
        }  
    }) 
	*/
}  

function executeSQL(name, sql, callback) {  
	/*
    plus.sqlite.selectSql({  
        name: name,  
        sql: sql,  
        success: function(e) {  
            // console.log("查询数据库:" + name + ",表:" + sql + ";的");  
            // console.log(JSON.stringify(e));  
            callback(e);  
        },  
        fail: function(e) {  
            console.log("查询数据库失败:" + JSON.stringify(e));  
            callback(e);  
        }  
    })
	*/
	
	db.transaction(function (tx) {
	    tx.executeSql(sql);
	},callback);
}

function executeSQLNew(name, sql) { 
	db.transaction(function (tx) {
	    tx.executeSql(sql);
	}, function(e) {  
             console.log("插入数据");  
             console.log(JSON.stringify(e)); 
        });
	/*
    plus.sqlite.executeSql({  
        name: name,  
        sql: sql,  
        success: function(e) {  
            // console.log("查询数据库:" + name + ",表:" + sql + ";的");  
            // console.log(JSON.stringify(e));  
        },  
        fail: function(e) {  
            console.log("查询数据库失败:" + JSON.stringify(e));  
            callback(e);  
        }  
    })
	*/
}

function beginTransaction(name) {  
   plus.sqlite.transaction({name: name,operation: 'begin'})
}

function commitTransaction(name) {  
   plus.sqlite.transaction({name: name,operation: 'commit'})
}
export{  
openComDB,  
executeSQL,
executeSQLNew,
beginTransaction,
commitTransaction
}